// event-bus.js
import { reactive } from 'vue';

const state = reactive({
    eventListeners: new Map()
});

function $on(event, callback) {
    const callbacks = state.eventListeners.get(event) || [];
    callbacks.push(callback);
    state.eventListeners.set(event, callbacks);
}

function $emit(event, ...args) {
    const callbacks = state.eventListeners.get(event);
    if (callbacks) {
        callbacks.forEach(callback => callback(...args));
    }
}

function $off(event, callback) {
    const callbacks = state.eventListeners.get(event);
    if (callbacks) {
        const index = callbacks.indexOf(callback);
        if (index !== -1) {
            callbacks.splice(index, 1);
        }
    }
}

export default {
    $on,
    $emit,
    $off
};